package com.example.shushiyunsst.fragment.main.home.child.miner.sub

import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.utils.QMUIDisplayHelper
import com.example.shushiyunsst.R
import com.google.android.material.bottomsheet.BottomSheetBehavior

/**
 * 矿池
 * */
class MinerPoolFragment : BaseFragmentEx() {

    companion object {
        fun newInstance(): MinerPoolFragment {
            val args = Bundle()

            val fragment = MinerPoolFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun getLayoutId() = R.layout.miner_pool_fragment

    private var headerNavigationLayoutHeight = 0

    private var mContentLayoutBehavior: BottomSheetBehavior<View>? = null

    private lateinit var mNavigationBg: View

    private val minerPoolContent = MinerPoolContentFragment.newInstance()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mNavigationBg = view.findViewById(R.id.mNavigationBg)

        val headerNavigationLayout: View = view.findViewById(R.id.headerLayout)
        headerNavigationLayout.setBackgroundColor(Color.TRANSPARENT)
        headerNavigationLayout.measure(0, 0)
        headerNavigationLayoutHeight = headerNavigationLayout.measuredHeight


        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.setTitle(R.string.miner_pool)
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        val mContentLayout: View = view.findViewById(R.id.mContentLayout)
        mContentLayout.measure(0, 0)


        minerPoolContent.setOnExpandListener(object : MinerPoolContentFragment.OnExpandListener {
            override fun onExpanding() {

                mContentLayoutBehavior?.state =
                    if (mContentLayoutBehavior?.state == BottomSheetBehavior.STATE_EXPANDED) {
                        BottomSheetBehavior.STATE_COLLAPSED
                    } else {
                        BottomSheetBehavior.STATE_EXPANDED
                    }

            }

        })
        loadRootFragment(R.id.mHeadLayout, MinerPoolHeaderFragment.newInstance())
        loadRootFragment(R.id.mContentLayout, minerPoolContent)

        mContentLayoutBehavior = BottomSheetBehavior.from(mContentLayout)

        mContentLayoutBehavior?.addBottomSheetCallback(mBottomSheetCallback)

        mContentLayoutBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED
    }

    private val mBottomSheetCallback =
        object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onStateChanged(bottomSheet: View, newState: Int) {

                val layoutParams = bottomSheet.layoutParams
                //如果控件本身的Height值就小于返回按钮的高度，就不用做处理
                val rootViewHeight =
                    rootView?.height ?: QMUIDisplayHelper.getScreenHeight(_mActivity)

                val bottomSheetNeedHeight = rootViewHeight - headerNavigationLayoutHeight

                if (bottomSheet.height > bottomSheetNeedHeight
                    && newState == BottomSheetBehavior.STATE_EXPANDED
                ) {
                    //屏幕高度减去marinTop作为控件的Height
                    layoutParams.height = bottomSheetNeedHeight
                    bottomSheet.layoutParams = layoutParams
                }
                minerPoolContent.panelStatus(newState)
            }

            override fun onSlide(bottomSheet: View, slideOffset: Float) {
                val alpha = if (slideOffset >= 0) {
                    slideOffset
                } else {
                    0f
                }

                mNavigationBg.alpha = alpha
            }

        }

    override fun onBackPressedSupport(): Boolean {
        pop()
        return true
    }

    override fun onDestroy() {
        super.onDestroy()
        mContentLayoutBehavior?.removeBottomSheetCallback(mBottomSheetCallback)
    }
}